%include "I:\workdata\706727\Build\Directories_And_SampleRestrictions.sas";
%include "I:\workdata\706727\Build\Programs.sas";
libname dstform "\\srvfsenas1\data\Formater\SAS formater i Danmarks Statistik\SAS_datasaet\Sundhed";
libname exp "I:\Workdata\706727\Build\Data";

data base (drop=befupdSourceYear);
set raw.befupdv (keep= pnr befupdSourceYear koen kom IE_TYPE FOED_DAG where=(befupdSourceyear>1998 and befupdSourceYear<2020 and befupdSourceyear-year(FOED_DAG)<=60 and befupdSourceyear-year(FOED_DAG)>=0));
year=BEFupdSourceYear;
length year 8;
age=year-year(foed_dag);
run;

%macro monthdata; 
  %do t=1 %to 12;
	  data base&t;
	  set base;
	  month=&t;
	  run;
%end;
%mend monthdata;
%monthdata;

data basedatayearmonth;
set %listsets(base,1,12);
run;

proc datasets;
delete base1 base2 base3 base4 base5 base6 base7 base8 base9 base10 base11 base12;
quit;

/** Identify mental health patients **/

/* Medicine data */
%macro meddata; 
  %do t=2000 %to 2019;
	  data med&t(keep=pnr  year psykmed);
	  set lmdb.lmdb&t.(where=(substr(ATC,1,3)="N05" or substr(ATC,1,3)="N06"));
	  year=year(eksd);
	  psykmed=1;
	  run;
%end;
%mend meddata;
%meddata;

data meddata;
set %listsets(med,2000,2019);
run;

proc sort data=meddata;
by pnr year ;
run;

proc means data=meddata nway noprint;
var psykmed;
by pnr year ;
output out=finalmeddata max=;
run;

data finalmeddata;
set finalmeddata;
* lag year by two *;
year=year+2;
run;

/* Doctor,psychiatrist,psychologist */

* 2000-2004 *;
data SYSI;
set raw.SYSIupdv (keep= pnr  speciale ydtyp ydernr afrper  bruhon ydlant where=(
(substr(afrper,1,2)>"94" or substr(afrper,1,2)<"05") and
(substr(speciale,1,3)="240" or substr(speciale,1,3)="241" or substr(speciale,1,3)="242" or substr(speciale,1,3)="243" or substr(speciale,1,3)="244" or substr(speciale,1,3)="245" or
substr(speciale,1,3)="260" or substr(speciale,1,3)="261" or substr(speciale,1,3)="262" or substr(speciale,1,3)="263" or substr(speciale,1,3)="264" or substr(speciale,1,3)="265" or
ydtyp="93" or ydtyp="94" or speciale="802149" or speciale="802148" or speciale="802147" or speciale="804106" or speciale="806101" 
)));
yr=substr(afrper,1,2)*1;
if yr<20 then year=yr+2000;
if yr>20 then year=yr+1900;
length year 8;
docpsyc=1;
run;

* 2005-2019 *;
data SSSY (  where=(year<2020));
set raw.SSSYupd01v (keep= pnr  speciale ydtyp ydernr afrper  bruhon ydlant where=( (
substr(speciale,1,3)="240" or substr(speciale,1,3)="241" or substr(speciale,1,3)="242" or substr(speciale,1,3)="243" or substr(speciale,1,3)="244" or substr(speciale,1,3)="245" or
substr(speciale,1,3)="260" or substr(speciale,1,3)="261" or substr(speciale,1,3)="262" or substr(speciale,1,3)="263" or substr(speciale,1,3)="264" or substr(speciale,1,3)="265" or
ydtyp="93" or ydtyp="94" or speciale="802149" or speciale="802148" or speciale="802147" or speciale="804106" or speciale="806101" 
)));
year=substr(afrper,1,2)*1+2000;
length year 8;
docpsyc=1;
run;

data sssy;
set sssy sysi;
run;

proc sort data=SSSY; by pnr year; run;

proc means data=SSSY  nway noprint;
by pnr year ;
var  docpsyc;
output out=GPvisits  max=;
run;

data GPvisits;
set GPvisits (keep= pnr year docpsyc);
* lag year by two *;
year=year+2;
run;

proc datasets;
delete sssy sssy2;
quit;

/* Psychiatric hospitals */
data psychosp (drop=c_adiag d_inddto  where=(year>1998 and year<2020));
set psyc.lpsypop2014 (keep= pnr D_inddto c_adiag where=(substr(c_adiag,1,3)="DF3" or substr(c_adiag,1,3)="DF4")) ;
year=year(d_inddto);
psychosp=1;
run;

proc sort data=psychosp; by pnr year; run;

proc means data=psychosp  nway noprint;
by pnr year ;
var  psychosp;
output out=fpsychosp  max=;
run;

data fpsychosp;
set fpsychosp (keep= pnr year psychosp);
* lag year by two *;
year=year+2;
run;

/* Merge */
proc sql;
create table basedata as select * from base as a

left join fpsychosp as b on a.pnr=b.pnr and a.year=b.year
left join finalmeddata as c on a.pnr=c.pnr and a.year=c.year
left join GPvisits as d on a.pnr=d.pnr and a.year=d.year;
quit;

data basedata;
set basedata;
mentalpat=(psychosp=1 or docpsyc=1 or psykmed=1);
pers=1;
run;


/* Identify relevant diagnoses at hospitals */
data pop(drop=LPRadmSourceYear);
set raw.LPRadmv (keep=pnr recnum LPRadmSourceYear d_inddto d_hendto c_adiag c_pattype c_spec c_indm c_henm
where=(LPRadmSourceYear>1998 and c_spec ne "50" and c_spec ne "52"));
year=year(d_inddto);
month=month(d_inddto);
run;

data pop;
set pop;
diag2=substr(c_adiag,1,4);
cont=1;
run;

proc sql;
create table hosp as select distinct pnr,year,diag2,max(cont) as mcont from pop 
group by pnr,year,diag2
order by pnr,year,diag2;
quit;


proc sql;
create table basehosp as select * from basedata as a

left join hosp as b on a.pnr=b.pnr and a.year=b.year;
quit;


proc sql;
create table numbyage as select distinct age, mentalpat, sum(pers) as pers from basedata
group by age,mentalpat
order by age,mentalpat;
quit;

proc sql;
create table numbyagediag as select distinct age,mentalpat,diag2, sum(pers) as diagpers from basehosp
group by age,mentalpat,diag2
order by age,mentalpat,diag2;
quit;

proc sql;
create table calcshares as select * from  numbyage as a

left join numbyagediag as b on a.age=b.age and a.mentalpat=b.mentalpat;
quit;

data calcshares;
set calcshares;
share=diagpers/pers;
run;

proc sort data=calcshares;
by age diag2;
run;

proc transpose data=calcshares out=cswide prefix=share;
by age diag2;
id mentalpat;
var share;
run;

data cswide;
set cswide;
odds=share1/share0;
run;

/* find age weights */
proc sql;
create table numbyagediag as select distinct age,diag2, sum(pers) as agediagspec from basehosp
group by age,diag2
order by age,diag2;
quit;

proc sql;
create table numbydiag as select distinct diag2, sum(pers) as diagspec from basehosp
group by diag2
order by diag2;
quit;

proc sql;
create table ageweight as select * from numbydiag as a
left join numbyagediag as b on a.diag2=b.diag2;
quit;

data ageweight(keep=age diag2 ageweight);
set ageweight;
ageweight=agediagspec/diagspec;
run;

proc sql;
create table cswide2 as select * from cswide as a

left join ageweight as b on a.age=b.age and a.diag2=b.diag2;
quit;

data cswide2;
set cswide2;
oddsw=odds*ageweight;
run;

proc sql;
create table finallist as select distinct diag2,sum(oddsw) as oddsw from cswide2
group by diag2
order by diag2;
run;

proc sql;
create table finallist2 as select * from finallist as a
left join numbydiag as b on a.diag2=b.diag2
left join dstform.c_icd10_l1l1_t as c on a.diag2=c.start;
quit;

data finallist3;
set finallist2 (where=(substr(diag2,2,1)~="F" and diagspec>1000));
run;

proc sort data=finallist3; by descending oddsw; run;

%let hospdiag=DJ69 DE87 DK72 DK71 DK70 DJ96 DJ22 DE64 DK59 DB10 DE86 DK86;
%let hospcont=DZ75 DZ51 DZ56 DZ73 DZ91 DZ74 DZ65;
%let hospsymp=DR41 DR40 DR68 DR11 DR52 DR56;
%let hosppois=DT43 DT42 DT50 DT48 DT40 DT96 DT65 DT39 DT46 DT36 DT51 DT45;

/** Medicine **/
%macro meddata; 
  %do t=1999 %to 2019;
	data med&t(keep=pnr year Med3 med);
	set lmdb.lmdb&t.(where=(substr(ATC,1,3)~="N05" and substr(ATC,1,3)~="N06"));
	year=year(eksd);
	Med3=substr(ATC,1,3);
	med=1;
	run;
	
	proc sql;
	create table medcol as select distinct pnr,year,Med3,max(med) as mcont from med&t 
	group by pnr,year,Med3
	order by pnr,year,Med3;
	quit;

	data med&t;
	set medcol;
	run;

%end;
%mend meddata;
%meddata;

data meddata;
set %listsets(med,1999,2019);
run;


proc sql;
create table basemed as select * from basedata as a

left join meddata as b on a.pnr=b.pnr and a.year=b.year;
quit;

proc sql;
create table numbyagemed as select distinct age,mentalpat,med3, sum(pers) as medpers from basemed
group by age,mentalpat,med3
order by age,mentalpat,med3;
quit;

proc sql;
create table calcshares as select * from  numbyage as a

left join numbyagemed as b on a.age=b.age and a.mentalpat=b.mentalpat;
quit;

data calcshares;
set calcshares;
share=medpers/pers;
run;

proc sort data=calcshares;
by age med3;
run;

proc transpose data=calcshares out=cswide prefix=share;
by age med3;
id mentalpat;
var share;
run;

data cswide;
set cswide;
odds=share1/share0;
run;

/* find age weights */
proc sql;
create table numbyagemed as select distinct age,med3, sum(pers) as agemedspec from basemed
group by age,med3
order by age,med3;
quit;

proc sql;
create table numbymed as select distinct med3, sum(pers) as medspec from basemed
group by med3
order by med3;
quit;

proc sql;
create table ageweight as select * from numbymed as a
left join numbyagemed as b on a.med3=b.med3;
quit;

data ageweight(keep=age med3 ageweight);
set ageweight;
ageweight=agemedspec/medspec;
run;

proc sql;
create table cswide2 as select * from cswide as a

left join ageweight as b on a.age=b.age and a.med3=b.med3;
quit;

data cswide2;
set cswide2;
oddsw=odds*ageweight;
run;

proc sql;
create table finallist as select distinct med3,sum(oddsw) as oddsw from cswide2
group by med3
order by med3;
run;

proc sql;
create table finallist2 as select * from finallist as a
left join numbymed as b on a.med3=b.med3
left join dstform.c_atc_l1l1_t as c on a.med3=c.start;
quit;

data finallist3;
set finallist2 ;
run;

proc sort data=finallist3; by descending oddsw; run;

/* Primary care */

/* 2000-2004 */;
data SYSI;
set raw.SYSIupdv (keep= pnr speciale afrper where=(substr(speciale,1,2)~="24" and substr(speciale,1,2)~="63"));
yr=substr(afrper,1,2)*1;
if yr<20 then year=yr+2000;
if yr>20 then year=yr+1900;
length year 8;
month=substr(afrper,3,2)*1;
cont=1;
run;

/* 2005-2016 */;
data SSSY (  where=(year<2017));
set raw.SSSYupd01v (keep= pnr  speciale afrper where=(substr(speciale,1,2)~="24" and substr(speciale,1,2)~="63"));
year=substr(afrper,1,2)*1+2000;
length year 8;
month=substr(afrper,3,2)*1;
cont=1;
run;

data sssy;
set sssy sysi;
run;

proc sql;
create table prim as select distinct pnr,year,speciale,max(cont) as mcont from sssy 
group by pnr,year,speciale
order by pnr,year,speciale;
quit;

proc sql;
create table baseprim as select * from basedata as a

left join prim as b on a.pnr=b.pnr and a.year=b.year;
quit;

proc sql;
create table numbyagespec as select distinct age,mentalpat,speciale, sum(pers) as specpers from baseprim
group by age,mentalpat,speciale
order by age,mentalpat,speciale;
quit;

proc sql;
create table calcshares as select * from  numbyage as a

left join numbyagespec as b on a.age=b.age and a.mentalpat=b.mentalpat;
quit;

data calcshares;
set calcshares;
share=specpers/pers;
run;

proc sort data=calcshares;
by age speciale;
run;

proc transpose data=calcshares out=cswide prefix=share;
by age speciale;
id mentalpat;
var share;
run;

data cswide;
set cswide;
odds=share1/share0;
run;

/* find age weights */
proc sql;
create table numbyagespec as select distinct age,speciale, sum(pers) as agespecspec from baseprim
group by age,speciale
order by age,speciale;
quit;

proc sql;
create table numbyspec as select distinct speciale, sum(pers) as specspec from baseprim
group by speciale
order by speciale;
quit;

proc sql;
create table ageweight as select * from numbyspec as a
left join numbyagespec as b on a.speciale=b.speciale;
quit;

data ageweight(keep=age speciale ageweight);
set ageweight;
ageweight=agespecspec/specspec;
run;

proc sql;
create table cswide2 as select * from cswide as a

left join ageweight as b on a.age=b.age and a.speciale=b.speciale;
quit;

data cswide2;
set cswide2;
oddsw=odds*ageweight;
run;

proc sql;
create table finallist as select distinct speciale,sum(oddsw) as oddsw from cswide2
group by speciale
order by speciale;
run;

proc sql;
create table finallist2 as select * from finallist as a
left join numbyspec as b on a.speciale=b.speciale
left join dstform.c_speciale_l1l1_t as c on a.speciale=c.start;
quit;

data finallist3;
set finallist2(where=(specspec>1000));
run;

proc sort data=finallist3; by descending oddsw; run;
